package core

import (
	"fmt"
	"gitee.com/hydrakyoufeng/emp/db"
	_ "github.com/go-sql-driver/mysql"
)

func insert(emp *Emp) {
	Db := db.Open();
	result,err := Db.Exec("insert into t_emp (name,sex,mobile,job) values (?,?,?,?)",emp.Name,emp.Sex,emp.Mobile,emp.Job)
	defer  Db.Close()
	if err != nil  {
		fmt.Println("发生数据库错误：" + err.Error())
		return
	}
	//fmt.Println("查询完毕！emp = ", emp)
	id,_ := result.LastInsertId();
	fmt.Println("新增主键：" , id)
	emp.TEmpPk = fmt.Sprint(id)

	rowsAffected,_ := result.RowsAffected()
	fmt.Println("增加成功！,影响行数：",rowsAffected)

}

func update(emp *Emp) {
	Db := db.Open();
	result,err := Db.Exec("update t_emp set name = ?,sex = ?,job = ?,mobile = ? where t_emp_pk = ? ",emp.Name,emp.Sex,emp.Job,emp.Mobile,emp.TEmpPk)
	defer  Db.Close()
	if err != nil {
		fmt.Println("发生数据库错误：" + err.Error())
		return
	}
	rowsAffected,_ := result.RowsAffected()
	fmt.Println("修改成功！,影响行数：",rowsAffected)
}
